package net.tngou.c4j.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.alibaba.druid.util.StringUtils;

import net.tngou.c4j.model.Style;
import net.tngou.crawler.util.StringUtil;

/**
 * 分解器
 * @author tngou@tngou.net
 *
 */
public class StyleUtil {

	private static String SIGN="####";
	public static List<Style> split(String str) {
		List<Style> list = new ArrayList<>();
		str=StringUtil.ReplaceBlank(str);
		String[] sts = Style.list;
		for (String string : sts) {
			str=StringUtil.replace(str, string+"[", SIGN+string+"[");
		}		
		String[] styles = StringUtil.split(str, SIGN);
		for (String string : styles) {
			Style style=_split(string);
			list.add(style);
		}
		return list;
	}
	
	private static Style  _split(String string) {
		   
			Style style= new Style();
		   List<String> list = new ArrayList<String>();
		   String[] sts =  StringUtil.split(string, "[");;
		   for (String st : sts) {
			   list.add(st.replace("]", "").replace("]", ""));
		   }
		   if(list.size()>0)
			   style.setType(list.get(0));
		   if(list.size()>1)
			   style.setName(list.get(1));
		   if(list.size()>2&&NumberUtils.isNumber(list.get(2)))
			   style.setSize(Integer.parseInt(list.get(2)));	
		   if(list.size()>3&&NumberUtils.isNumber(list.get(3)))
			   style.setSize(Integer.parseInt(list.get(3)));
		   return style;
		
	}

}
